package com.example.myapplication;

import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import java.util.List;

public class MainActivity extends AppCompatActivity {

    // 通过资源id 访问视图对象
    Button btn1;
    Button btn2;
    Button next;
    int currentIndex = 0;
    TextView questionTextView;
    List<Question> questionBank = List.of(
        new Question(R.string.question_australia, true),
        new Question(R.string.question_oceans, true),
        new Question(R.string.question_oceans, true),
        new Question(R.string.question_mideast, false),
        new Question(R.string.question_africa, false),
        new Question(R.string.question_americas, true),
        new Question(R.string.question_asia, true)
    );

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        btn1 = findViewById(R.id.btn1);
        btn2 = findViewById(R.id.btn2);
        next = findViewById(R.id.next);
        questionTextView = findViewById(R.id.question_text_view);

        btn1.setOnClickListener(v -> {
            checkAnswer(true);
        });
        btn2.setOnClickListener(v -> {
            checkAnswer(false);
        });
        next.setOnClickListener(
                v -> {
                    currentIndex = (currentIndex+1)%questionBank.size();
                    updateQuestion();
                }
        );
        updateQuestion();
    }

    /**
     * 更新 问题文本
     */
   private void updateQuestion(){
       int questionTextResId = questionBank.get(currentIndex).getTextResId();
       questionTextView.setText(questionTextResId);
   }
    /**
     * 检查是否正确
     */
    private  void  checkAnswer(boolean userAnswer){
        boolean correctAnswer = questionBank.get(currentIndex).isAnswer();
        int messageResId;
        if(correctAnswer==userAnswer){
            messageResId = R.string.correct_toast;
        }else{
            messageResId = R.string.incorrect_toast;
        }
        Toast.makeText(this,messageResId,Toast.LENGTH_SHORT).show();
    }
}